package ua.com.minus1.lookbattle;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import com.actionbarsherlock.app.SherlockFragment;
import ua.com.minus1.lookbattle.models.Country;
import ua.com.minus1.lookbattle.utils.AppPreferences;

/**
 * Created with IntelliJ IDEA.
 * User: Igorek
 * Date: 11/2/12
 * Time: 11:01 PM
 * To change this template use File | Settings | File Templates.
 */
public class AddLookConfirmFragment extends SherlockFragment implements View.OnClickListener, CountrySelectorDialogFragment.Callbacks {

    private ImageView mImage;
    private Button mCountryBtn;
    private AppPreferences mAppPreferences;

    public static AddLookConfirmFragment newInstance(String path) {
        Bundle args = new Bundle();
        args.putString("imagePath", path);

        AddLookConfirmFragment f = new AddLookConfirmFragment();
        f.setArguments(args);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_add_look_confirm, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mAppPreferences = (AppPreferences) new AppPreferences(getActivity());
        updateCountryText();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mImage = (ImageView) view.findViewById(R.id.add_look_image);
        mCountryBtn = (Button) view.findViewById(R.id.add_look_country_btn);
        mCountryBtn.setOnClickListener(this);
        updatePicture();
        updateCountryText();
    }

    public void onClick(View view) {
        showCountriesSelectorDialog();
    }

    private void showCountriesSelectorDialog() {
        FragmentManager fm = getFragmentManager();

        CountrySelectorDialogFragment countrySelectorDialog = CountrySelectorDialogFragment.newInstance();
        countrySelectorDialog.setTargetFragment(this, 0);
        countrySelectorDialog.setCallbacks(this);
        countrySelectorDialog.show(fm, "country_selector_dialog");
    }

    public void onCountrySelect(Country country) {
        mAppPreferences.setCountry(country);
        updateCountryText();
    }

    private void updateCountryText() {
        if(mCountryBtn != null && mAppPreferences != null && mAppPreferences.getCountry() != null)
            mCountryBtn.setText(mAppPreferences.getCountry().name);
    }

    private void updatePicture() {
        String path = getArguments().getString("imagePath");
        mImage.setImageBitmap(BitmapFactory.decodeFile(path));
    }
}
